
local beibian = fk.CreateSkill{
  name = "xiaobai__beibian",
  attached_kingdom = {"shu"},
  tags = {
    Skill.AttachedKingdom
  }
}

Fk:loadTranslationTable{
  ["xiaobai__beibian"] = "狈变",
  [":xiaobai__beibian"] = "蜀势力技，你可以将一张非基本牌当【决斗】使用（此牌类型须与上次发动此技能转化用的底牌类型不同），"..
                          "此牌的响应方式添加“询问对方是否将势力变更至魏”直到一方拒绝/双方同意后，此牌伤害+1/双方各摸两张牌。",

  ["@beibian-type"] = "狈变",
  ["#shenyi_do_change_country"] = "决斗：是否变更势力至魏以相应此牌",
  ["shenyi_duel_use"] = "狈变:你可以将一张非基本牌当【决斗】使用，此【决斗】可以被“变更势力至魏”响应",

  ["$xiaobai__beibian1"] = "魏王代汉，天地所兆，吾欲从之久矣！",
  ["$xiaobai__beibian2"] = "吾诚小人，虽不能始终，亦颇识去就之分。",
}

local shenyiDuelSkill = fk.CreateSkill{
  name = "duel",
}

shenyiDuelSkill:addEffect("cardskill",{
  prompt = "#shenyi_duel-prompt",
  mod_target_filter = function(self, player, to_select, selected, card)
    return to_select ~= player
  end,
  target_filter = Util.CardTargetFilter,
  target_num = 1,
  on_effect = function(self, room, effect)
    local to = effect.to
    local from = effect.from
    local responsers = { to, from }
    local currentTurn = 1
    local currentResponser = to
    local do_change = true
    local do_changes = {}
    while currentResponser:isAlive() do
      local loopTimes = effect:getResponseTimes(currentResponser)
      local respond = false
      if currentResponser.kingdom ~= "wei" and do_change then
        do_change = room:askToSkillInvoke(currentResponser, {skill_name = "duel", prompt = "#shenyi_do_change_country"})
        if not do_change then
          effect.additionalDamage = (effect.additionalDamage or 0) + 1
        else
          room:changeKingdom(currentResponser, "wei", true)
          do_changes[currentTurn] = true
          if do_changes[1] and do_changes[2] then
            do_change = false
            room:drawCards(from, 2, beibian.name)
            room:drawCards(to, 2, beibian.name)
          end
          respond = true
        end
      end
      if not respond then
        for i = 1, loopTimes do
          local params = { ---@type AskToUseCardParams
            skill_name = 'slash',
            pattern = 'slash',
            cancelable = true,
            event_data = effect
          }
          if loopTimes > 1 then
            params.prompt = "#AskForResponseMultiCard:::slash:"..i..":"..loopTimes
          end
          local res = room:askToResponse(currentResponser, params) 
          if res then
            room:responseCard(res)
            respond = true
          else
            break
          end
        end

        if not respond then
          break
        end
      end
      currentTurn = currentTurn % 2 + 1
      currentResponser = responsers[currentTurn]
    end

    if currentResponser:isAlive() then
      room:damage({
        from = responsers[currentTurn % 2 + 1],
        to = currentResponser,
        card = effect.card,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = "duel_skill",
      })
    end
  end,
})


beibian:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "duel",
  prompt = "shenyi_duel_use",
  card_filter = function (self, player, to_select, selected)
    local card = Fk:getCardById(to_select)
    return #selected < 1 and card.type ~= Card.TypeBasic and card:getTypeString() ~= Self:getMark("@beibian-type")
  end,
  card_num = 1,
  view_as = function (self, player, cards)
    if #cards > 1 or #cards == 0 then return nil end  
    local card = Fk:cloneCard("duel")
    card.skillName = beibian.name
    card:addSubcards(cards)
    return card
  end,
  after_use = function (self, player, use)
    player.room:setPlayerMark(player, "@beibian-type", Fk:getCardById(use.card.subcards[1]):getTypeString())
  end
})

beibian:addEffect(fk.PreCardEffect, {
  can_refresh = function (self, event, target, player, data)
    return table.contains(data.card.skillNames, beibian.name)
  end,
  on_refresh = function (self, event, target, player, data)
    data:changeCardSkill(shenyiDuelSkill.name)
  end
})

local skels = {}
table.insert(skels, beibian)
table.insert(skels, shenyiDuelSkill)

return skels